package com.xneure.code.excel;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import com.xneure.code.upload.FileCheckException;
import com.xneure.code.upload.FileUploadParam;
import com.xneure.code.upload.Handling;
import com.xneure.code.upload.UploadFileHandie;
import com.xneure.utils.ObjectUtil;
import com.xneure.utils.SpringUtil;

public class ExcelHandie extends UploadFileHandie{

	private ExcelImportProvide parse;

	@Override
	public void initParam(FileUploadParam param){
		super.initParam(param);
		ExcelImportHandler obj;
		String paramValue = param.getParamValue("handie");
		obj = SpringUtil.getBean(paramValue);
		if(null == obj){
			obj = ObjectUtil.newInstance(paramValue);
		}
		obj.setParam(param.getParaMap());
		parse = new ExcelImportProvide(obj);
	}

	@Override
	public void checkType(){
		String type = param.getParamValue("fileName");
		if(type.endsWith(".xls") == false && type.endsWith(".xlsx") == false){
			throw new FileCheckException("文件格式错误，请上传有效的Excel文件。 ");
		}
	}

	@Override
	public Handling getHandling(){
		return Handling.IGNORE;
	}

	@Override
	public int getReadSize(){
		return 0;
	}

	@Override
	public String getSavePath(){
		return null;
	}

	@Override
	public void checkSize(){
	}

	@Override
	public File checkFile(){
		return null;
	}

	@Override
	public void checkPath(){
	}

	@Override
	public void downloadFile(OutputStream os){
	}

	@Override
	public String getSaveFileName(){
		return null;
	}

	@Override
	public File saveFile(InputStream inputStream,File sFile) throws Exception{
		parse.loadData(inputStream);
		return sFile;
	}

	@Override
	public Object returnParam(){
		return parse.getHandler().getParam();
	}
}
